package com.dareway.jvm;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

public class weak_reference {
    public static void main(String[] args) {
        SoftReference<Object> softReference = new SoftReference<>(new Object());
        WeakReference<Object> weakReference = new WeakReference<>(new Object());

        //手动GC
        System.gc();

        System.out.println("软引用对象：" + softReference.get());
        System.out.println("弱引用对象：" + weakReference.get());


        Integer a = new Integer(1);
//        如果WeakHashMap中的Key没有其他引用那么此Map会自动丢弃此键值对
        WeakHashMap<Integer, String> weakHashMap = new WeakHashMap<>();
        weakHashMap.put(a, "yyds");
        System.out.println(weakHashMap);

        a = null;
        System.gc();

        System.out.println(weakHashMap);
    }
}
